﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Controllers;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Forums.Domain;
	using Falcon.Forums.Domain.Services;
	using Falcon.Forums.Domain.Values;

	public class SystemForumController : DefaultController
	{
		public SystemForumController(IForumService forumService)
		{
			this.ForumService = forumService;
		}

		public IForumService ForumService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index()
		{
			var forums = ForumService.GetAll();

			return View(Views.Index, Masters.System, forums);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Index(ForumUpdateOrder[] values)
		{
			var forum = ForumService.Create();

			if (!forum.CanUpdate)
			{
				return NotAuthorized();
			}

			ForumService.Update(values);

			return base.RedirectToRoute(ForumsRoutes.SystemForumIndex);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Create()
		{
			var forum = ForumService.Create();

			return forum.CanCreate ? View(Views.Create, Masters.System, new ForumCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize]
		public ActionResult Create(ForumCreate value)
		{
			try
			{
				var forum = ForumService.Create();

				if (!forum.CanCreate)
				{
					return NotAuthorized();
				}

				this.ForumService.Insert(forum, value);

				return Message(ForumsRoutes.SystemForumUpdate, new { id = forum.Id }, ForumsResources.Messages.ForumCreated.Fmt(forum.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Update(int id)
		{
			var forum = ForumService.GetById(id);

			if (forum == null)
			{
				return Error(ForumsResources.Messages.ForumNotFound);
			}

			return forum.CanUpdate ? View(Views.Update, Masters.System, new ForumUpdate(forum)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize]
		public ActionResult Update(ForumUpdate value)
		{
			try
			{
				var forum = ForumService.GetById(value.Id);

				if (forum == null)
				{
					return Error(ForumsResources.Messages.ForumNotFound);
				}

				if (!forum.CanUpdate)
				{
					return NotAuthorized();
				}

				this.ForumService.Update(forum, value);

				return Message(ForumsRoutes.SystemForumUpdate, new { id = forum.Id }, ForumsResources.Messages.ForumUpdated.Fmt(forum.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Delete(int id)
		{
			var forum = this.ForumService.GetById(id);

			if (forum == null)
			{
				return base.Error(ForumsResources.Messages.ForumNotFound);
			}

			return forum.CanDelete ? base.View(Views.Delete, Masters.System, new ForumDelete(forum)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize]
		public ActionResult Delete(ForumDelete value)
		{
			try
			{
				var forum = this.ForumService.GetById(value.Id);

				if (forum == null)
				{
					return base.Error(ForumsResources.Messages.ForumNotFound);
				}

				if (!forum.CanDelete)
				{
					return NotAuthorized();
				}

				this.ForumService.Delete(forum);

				return base.RedirectToRoute(ForumsRoutes.SystemForumIndex);
			}
			catch (ValidationException exception)
			{
				base.ModelState.CopyValidationExceptions(exception);
			}

			return base.View(Views.Delete, Masters.System, value);
		}
	}
}